/*
Copyright © 2022 duduniao

*/
package cmd

import (
	"fmt"
	"gosh/config"
	"gosh/pkg/push"
	"os"
	"time"

	"github.com/spf13/cobra"
)

// pushCmd represents the push command
var pushCmd = &cobra.Command{
	Use:   "push {-i hosts.txt|-H host01,host02,...} [flags] localFile... remoteDirectory",
	Short: "发送文件到远程主机",
	Long:  `发送一个或者多个文件到远程主机`,
	Args:  cobra.MinimumNArgs(2),
	Run: func(cmd *cobra.Command, args []string) {
		config.SSHInfo.Args = args
		if err := config.SSHInfo.Parse(); err != nil {
			fmt.Println(err.Error())
			os.Exit(1)
		}
		push.Run()
	},
}

func init() {
	rootCmd.AddCommand(pushCmd)
	pushCmd.Flags().StringVarP(&config.SSHInfo.Inventory, "inventory", "i", "", "指定主机清单列表文件,# 开头为注释")
	pushCmd.Flags().StringVarP(&config.SSHInfo.Host, "host", "H", "", "指定主机列表，使用逗号分隔")
	pushCmd.Flags().IntVarP(&config.SSHInfo.Port, "port", "p", 22, "指定远程主机ssh的端口")
	pushCmd.Flags().StringVarP(&config.SSHInfo.User, "user", "u", "", "远程ssh用户,默认当前用户")
	pushCmd.Flags().StringVarP(&config.SSHInfo.Password, "password", "P", "", "远程ssh用户的密码")
	pushCmd.Flags().StringVarP(&config.SSHInfo.Key, "key", "k", "~/.ssh/id_rsa", "指定ssh私钥")
	pushCmd.Flags().StringVarP(&config.SSHInfo.KeyPassphrase, "key.passphrase", "", "", "ssh私钥的密码")
	pushCmd.Flags().IntVarP(&config.SSHInfo.Fork, "fork", "f", 10, "指定并发数量")
	pushCmd.Flags().StringVarP(&config.SSHInfo.BastionHost, "bastion.host", "", "", "指定跳板机IP")
	pushCmd.Flags().IntVarP(&config.SSHInfo.BastionPort, "bastion.port", "", 22, "指定跳板机端口")
	pushCmd.Flags().StringVarP(&config.SSHInfo.BastionUser, "bastion.user", "", "", "指定跳板机用户,默认当前用户")
	pushCmd.Flags().StringVarP(&config.SSHInfo.BastionPassword, "bastion.password", "", "", "指定跳板机密码")
	pushCmd.Flags().StringVarP(&config.SSHInfo.BastionKey, "bastion.key", "", "~/.ssh/id_rsa", "指定跳板机ssh私钥")
	pushCmd.Flags().StringVarP(&config.SSHInfo.BastionKeyPassphrase, "bastion.key.passphrase", "", "", "指定跳板机ssh私钥的密码")
	pushCmd.Flags().DurationVarP(&config.SSHInfo.ConnectTimeout, "connect.timeout", "t", time.Second*5, "ssh连接超时时间")
	pushCmd.Flags().StringSliceVarP(&config.SSHInfo.PrintFields, "print.field", "", []string{"ip", "stdout", "stderr"}, "选择打印的消息字段")
	pushCmd.Flags().StringSliceVarP(&config.SSHInfo.PrintStatus, "print.status", "", []string{"failed", "succeed"}, "选择打印成功或者失败的消息")
}
